package com.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.CourseDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.ClassPO;
import com.po.CoursePO;

@Controller
public class CourseAction {
	@Autowired
	private CourseDao cd;
	private List<ClassPO> listCourse;
	private List<CoursePO> listCourseDetail;
	
	/**
	 * 展示课程信息
	 * @return
	 */
	
	public String show() {
		listCourse = cd.findCourseInfo();
		ActionContext.getContext().put("listCourse", listCourse);
		return "course";
	}
	
	/**
	 * 展示课程的详细信息
	 * @return
	 */
	public String showDetail(){
		//拿到班级的ID 
	HttpServletRequest request = ServletActionContext.getRequest();	
		int id;
	
		if (request.getParameter("cid") != null) {
			 id = Integer.parseInt(request.getParameter("cid"));
		}else{
		     id = (int) request.getAttribute("pid");	
		}
	
		//将班级的ID，保存在request域中
	request.setAttribute("id", id);
	//根据班级的ID找到相应的详细课程	
	listCourseDetail = cd.findLevelInfoById(id);
	ActionContext.getContext().put("listCourseDetail", listCourseDetail);
	return "level";
	}
	
	/**
	 * 删除课程信息
	 * @return
	 */
	public String delete() {
		// 获取课程ID
		HttpServletRequest req = ServletActionContext.getRequest();
		int id = Integer.parseInt(req.getParameter("id"));

		cd.deleteCourseInfo(id);

		return "after_delete";
	}
	/**
	 * 修改课程信息回显数据
	 * @return
	 */
	public String updateEcho(){
		//获取课程的Id
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		ClassPO classPO = new ClassPO();
		classPO = cd.findCourseInfoById(id);
		
		request.setAttribute("id", id);
		ActionContext.getContext().put("coursePO", classPO);
		
		return "updateEcho";
	}
	
	
	
	
	
	
	/**
	 * 修改课程信息
	 */
	public String update() {
		HttpServletRequest request = ServletActionContext.getRequest();

		Map<String, String> map = new HashMap<String, String>();
		map.put("id", request.getParameter("id"));
		map.put("name", request.getParameter("name"));
		map.put("intro", request.getParameter("intro"));
		cd.updateCourseInfo(map);

		return null;
	}
	
}
